package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategory3;
import com.atguigu.gmall.product.mapper.BaseCategory3Mapper;
import com.atguigu.gmall.product.service.BaseCategory3Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 2级分类的实现类
 */
@Service
public class BaseCategory3ServiceImpl extends ServiceImpl<BaseCategory3Mapper, BaseCategory3>
        implements BaseCategory3Service {
    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    /**
     * 条件查询
     *
     * @param baseCategory3
     * @return
     */
    @Override
    public List<BaseCategory3> search(BaseCategory3 baseCategory3) {
        //判断查询参数是否为空
        if (baseCategory3 == null){
            return baseCategory3Mapper.selectList(null);
        }
        QueryWrapper wrapper = getCondition(baseCategory3);
        //执行查询
        List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(wrapper);
        return baseCategory3s;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<BaseCategory3> pageSearch(Integer page, Integer size) {
        //执行查询
        IPage<BaseCategory3> result = baseCategory3Mapper.selectPage(new Page<>(page,size),null);
        //返回结果
        return result.getRecords();
    }

    /**
     * 条件分页查询
     *
     * @param baseCategory3
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<BaseCategory3> search(BaseCategory3 baseCategory3, Integer page, Integer size) {
        QueryWrapper wrapper = getCondition(baseCategory3);
        //执行查询
        IPage<BaseCategory3> result = baseCategory3Mapper.selectPage(new Page<>(page,size),wrapper);
        //返回结果
        return result.getRecords();
    }

    private QueryWrapper getCondition(BaseCategory3 baseCategory3){
        //定义条件构造器
        QueryWrapper<BaseCategory3> wrapper = new QueryWrapper<>();
        //若id不为空
        if (baseCategory3.getId() !=null){
            wrapper.eq("id",baseCategory3.getId());
        }
        //若name不为空
        if (StringUtils.isNotEmpty(baseCategory3.getName())){
            wrapper.like("name",baseCategory3.getName());
        }
        return wrapper;
    }

}
